var path = require('path'),
    webpack = require('webpack'),
    config = require('./config'),
    srcDir = path.join(__dirname, 'client'),
    imagePath = path.join(srcDir, 'img'),
    cssPath = path.join(srcDir, 'css'),
    nodeModulesPath = path.join(__dirname, 'node_modules'),
    autoprefixer = require('autoprefixer'),
    publicPath = config.domain + '/',
    CopyWebpackPlugin = require('copy-webpack-plugin'),
    os = require('os'),
    CleanPlugin = require('clean-webpack-plugin'),
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");

module.exports = {
    cache: true,
    entry: {
         index: ['babel-polyfill', path.join(__dirname, "client/views/index.jsx")]
    },
    output: {
        publicPath: publicPath,
        path: path.join(__dirname, "build/static/"),
        filename: "core/[name].js",
        chunkFilename: "core/[name].js"
    },
    resolve: {
        extensions: ['.js', '.jsx', '.json'],
        alias: {
            'img': path.join(srcDir, 'img'),
            'css': path.join(srcDir, 'css'),
            'js': path.join(srcDir, 'js'),
            'views': path.join(srcDir, 'views')
        }
    },
    module: {
        rules: [{
            test: /\.(png|jpg|gif)$/,
            include: [imagePath],
            use: [{
                loader: 'style-loader',
                options: {
                    limit: 1000,
                    name: 'img/[name].[hash:5].[ext]'
                }
            }]
        }, {
            test: /\.css$/,
            use: ["style-loader", {
                loader: "css-loader",
                options: {
                    sourceMap: true
                }
            }, {
                loader: 'postcss-loader',
                options: {
                    plugins: [autoprefixer]
                }
            }]
        }, {
            test: /\.scss$/,
            use: ["style-loader", {
                loader: "css-loader",
                options: {
                    sourceMap: true
                }
            }, {
                loader: 'postcss-loader',
                options: {
                    plugins: [autoprefixer]
                }
            }, {
                loader: "sass-loader",
                options: {
                    sourceMap: true,
                    imagePath: imagePath,
                    includePaths: [cssPath]
                }
            }],
        }, {
            test: /\.jsx$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            use: [{
                loader: "babel-loader",
                options: {
                    presets: ['react', 'es2015']
                }
            }],
        }, {
            test: /\.js$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            use: [{
                loader: "babel-loader",
                options: {
                    presets: ['es2015']
                }
            }],
        }]
    },
    plugins: [
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./build/static/dll/vendor-manifest.json')
        }),
        new CopyWebpackPlugin([{
            from: path.join(__dirname, 'client/js/lib'),
            to: path.join(__dirname, 'build/static/lib')
        }]),
        new HtmlWebpackPlugin({
            filename: path.join(__dirname, 'build/html/index.html'),
            template: path.join(__dirname, 'build/html/index_dll.html'),
            inject: true
        })
    ]
};